import logging
import time

from websockets.legacy.server import WebSocketServerProtocol
import asyncio
import websockets
import threading
logging.basicConfig(level=logging.INFO)
clients = set()
msg = None
async def ws_handle(websocket: WebSocketServerProtocol, path: '/sumo/result/1'):
    # if(websocket in clients):
    global clients, msg
    clients.add(websocket)
    logging.info(f'{websocket.remote_address} connects')
    try:
        async for message in websocket:
            msg = message
            print(msg)
    finally:

        clients.remove(websocket)


async def sendmsg(mssage):
    # if(websocket in clients):
    global clients
    for client in clients:
        # print(mssage)
        await client.send(mssage)


async def test(mssage):
    await sendmsg(mssage)  # 依次给每个客户端都发一条信息


def asyntest(mssage):
    asyncio.run(test(mssage))


async def main():
    async with websockets.serve(ws_handle, "10.100.84.194", 8083,ping_interval=None):
    # async with websockets.serve(ws_handle, "127.0.0.1", 8085):
        await asyncio.Future()  # run forever


def WebSocketServer():
    asyncio.run(main())


thread3 = threading.Thread(target=WebSocketServer)
thread3.start()
# # 主函数
